package com.supor.aiot.mine.ui.cooking;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.android.baseconfig.base.GlideApp;
import com.supor.aiot.common.arouter.PathUrl;
import com.supor.aiot.common.base.BaseMVVMEnhanceActivity;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.common.utils.NotNullUtils;
import com.supor.aiot.mine.R;
import com.supor.aiot.mine.bean.CookingEvalDetail;
import com.supor.aiot.mine.bean.CookingMediaSelectBean;
import com.supor.aiot.mine.databinding.ActivityCookingEvalDetailBinding;
import com.supor.aiot.mine.ui.adapter.CookingCommXGraidAdapter;

import java.util.ArrayList;

import androidx.lifecycle.Observer;

@Route(path = PathUrl.HomeMine.PATH_MINE_COOKING_EVAL_DETAIL)
public class CookingEvalDetailActivity extends BaseMVVMEnhanceActivity<CookingRecordModel, ActivityCookingEvalDetailBinding> {
    private String id;
    private String evalName;
    private String evalImg;
    private CookingCommXGraidAdapter cookingCommXGraidAdapter;

    @Override
    protected void initData() {
        super.initData();
        bindingView.flPageBack.setOnClickListener(view -> finish());
        if (null != getIntent()) {
            id = getIntent().getStringExtra("COOKING_EVAL_ID");
            evalName = getIntent().getStringExtra("COOKING_EVAL_NAME");
            evalImg = getIntent().getStringExtra("COOKING_EVAL_IMG");
        }
        bindingView.tvCookingName.setText(NotNullUtils.getNotNullValue(evalName));
        GlideApp.with(this)
                .load(evalImg)
                .into(bindingView.ivCookingImg);
        viewModel.getCookingEvalDetailResult().observe(this, new Observer<Result<CookingEvalDetail>>() {
            @Override
            public void onChanged(Result<CookingEvalDetail> cookingEvalDetailResult) {
                if (null != cookingEvalDetailResult.getData().getRows()
                        && cookingEvalDetailResult.getData().getRows().size() > 0) {
                    CookingEvalDetail.RowsDTO bean = cookingEvalDetailResult.getData().getRows().get(0);
                    switch (bean.getSatisfaction()) {
                        case 1:
                            bindingView.ivEvalLevel.setBackgroundResource(R.mipmap.ic_eval_level_ordinary_selected);
                            bindingView.tvEvalLevel.setText("一般般");
                            break;
                        case 2:
                            bindingView.ivEvalLevel.setBackgroundResource(R.mipmap.ic_eval_level_notbad_selected);
                            bindingView.tvEvalLevel.setText("还不错");
                            break;
                        case 3:
                            bindingView.ivEvalLevel.setBackgroundResource(R.mipmap.ic_eval_level_fantastic_selected);
                            bindingView.tvEvalLevel.setText("太赞了");
                            break;
                    }

                    if (!TextUtils.isEmpty(bean.getSatisfiedLabels())) {
                        String[] strs = bean.getSatisfiedLabels().split(",");
                        for (int i = 0; i < strs.length; i++) {
                            addTag(strs[i]);
                        }
                    }

                    ArrayList<CookingMediaSelectBean> mediaData = new ArrayList<>();
                    if (!TextUtils.isEmpty(bean.getEvaluateImage())) {
                        String[] strs = bean.getEvaluateImage().split(",");
                        for (int i = 0; i < strs.length; i++) {
                            CookingMediaSelectBean cookingMediaSelectBean = new CookingMediaSelectBean();
//                            cookingMediaSelectBean.setItemType(CookingMediaSelectBean.TYPE_ITEM_IMG);
                            cookingMediaSelectBean.setPath(strs[i]);
                            mediaData.add(cookingMediaSelectBean);
                        }

                    } else if (!TextUtils.isEmpty(bean.getEvaluateVideo())) {
                        String[] strs = bean.getEvaluateVideo().split(",");
                        CookingMediaSelectBean cookingMediaSelectBean = new CookingMediaSelectBean();
//                        cookingMediaSelectBean.setItemType(CookingMediaSelectBean.TYPE_ITEM_VIDEO);
                        cookingMediaSelectBean.setPath(strs[0]);
                        mediaData.add(cookingMediaSelectBean);
                    }

                    bindingView.tvContent.setText(TextUtils.isEmpty(bean.getSupplementary()) ? "什么也没留下~" : bean.getSupplementary());
                    cookingCommXGraidAdapter = new CookingCommXGraidAdapter(CookingEvalDetailActivity.this,
                            mediaData, R.layout.item_cooking_comm_media);
                    bindingView.xgrid.setAdapter(cookingCommXGraidAdapter);
                    // bindingView.tvSynState.setVisibility(bean.getNote() == 1 ? View.VISIBLE : View.GONE);
                    bindingView.tvSynState.setVisibility(View.GONE);
                }
            }
        });

        viewModel.getCookingEvalDetail(id, bindDestoryAutoDispose());
    }

    private void addTag(String tag) {
        if (TextUtils.isEmpty(tag)) {
            return;
        }
        TextView tagView = (TextView) View.inflate(this, R.layout.tag_cooking_eval, null);
        tagView.setTextColor(0xFF16BF86);
        tagView.setText(tag);
        tagView.setBackgroundResource(R.drawable.shape_cooking_eval_tag_light_bg);
        bindingView.warpLinearLayout.addView(tagView);
    }

    @Override
    protected void initView() {
        super.initView();


    }
}